using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;

namespace G1.EngineV
{
    public abstract class Scene
    {

        private SpriteManager _SpriteManager;

        public Scene()
        {
            _SpriteManager = new SpriteManager();
        }

        public void AddSprite( Sprite Sprite )
        {
            lock ( _SpriteManager.Sprites )
            {

                _SpriteManager.Sprites.Add( Sprite );

            }
        }

        public void RemoveSprite( Sprite Sprite )
        {
            lock ( _SpriteManager.Sprites )
            {

                _SpriteManager.Sprites.Remove( Sprite );

            }
        }

        public void Render( Android.Graphics.Canvas Canvas )
        {
            lock ( _SpriteManager.Sprites )
            {

                for ( int i=0; i<_SpriteManager.Sprites.Count(); i++ )
                    if ( _SpriteManager.Sprites[i].Visible )
                        _SpriteManager.Sprites[i].Render( Canvas );

            }
        }

        public void Update()
        {

            lock ( _SpriteManager.Sprites )
            {

                for ( int i = 0; i < _SpriteManager.Sprites.Count(); i++ )
                    if ( _SpriteManager.Sprites[i].Visible )
                        _SpriteManager.Sprites[i].Update();

            }
        }

        private TouchPath _CurrentTouchPath;
        private bool _TouchedDown = false;

        public System.Collections.Generic.IEnumerable<Sprite> FindSpritesByTag( String Tag )
        {
            for ( int i = 0; i < _SpriteManager.Sprites.Count; i++ )
            {

                if ( _SpriteManager.Sprites[i].Tag.Equals( Tag ) )
                    yield return _SpriteManager.Sprites[i];

            }
        }

        public void Touch( MotionEvent e )
        {
            lock ( _SpriteManager.Sprites )
            {

                for ( int i = _SpriteManager.Sprites.Count() - 1; i >= 0; i-- )
                    if ( _SpriteManager.Sprites[i].Visible )
                        if ( typeof( ITouchable ).IsAssignableFrom( _SpriteManager.Sprites[i].GetType() ) )
                        {


                            switch ( e.Action )
                            {
                                case MotionEventActions.Down:

                                    if ( _SpriteManager.Sprites[i].TestPoint( e.GetX(), e.GetY() ) )
                                    {

                                        _TouchedDown = true;

                                        _CurrentTouchPath = new TouchPath();
                                        _CurrentTouchPath.Add( ( int )e.GetX(), ( int )e.GetY() );

                                    }

                                    break;

                                case MotionEventActions.Move:

                                    if ( _TouchedDown )
                                        _CurrentTouchPath.Add( ( int )e.GetX(), ( int )e.GetY() );

                                    break;

                                case MotionEventActions.Up:


                                    if ( _TouchedDown )
                                    {
                                        _CurrentTouchPath.Add( ( int )e.GetX(), ( int )e.GetY() );
                                        ( ( ITouchable )_SpriteManager.Sprites[i] ).OnTouched( _CurrentTouchPath );

                                        _TouchedDown = false;
                                    }

                                    break;
                            }

                            

                        }

            }
        }

        public abstract void Init();
        public abstract void Start();
        public abstract void Stop();
        
    }
}